package com.feng.leetcode;

import java.util.*;

/**
 * @author feng
 * @decrition
 * @date 2022/11/1523:27
 */
public class L1710 {
    public static int maximumUnits(int[][] boxTypes, int truckSize) {
        TreeMap<Integer, Integer> map = new TreeMap<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        for (int i = 0; i < boxTypes.length; i++) {
            map.put(boxTypes[i][1], map.getOrDefault(boxTypes[i][1], 0) + boxTypes[i][0]);
        }
        Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
        Iterator<Map.Entry<Integer, Integer>> iterator = entries.iterator();
        int result = 0;
        while (iterator.hasNext()) {
            Map.Entry<Integer, Integer> next = iterator.next();
            if (truckSize <= 0) {
                break;
            }
            Integer key = next.getValue();
            if (truckSize >= key) {
                truckSize = truckSize - key;
                result += next.getKey() * key;
            } else {
                result += next.getKey() * truckSize;
                break;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[][] box = {{1,3},{2,2},{3,1}};
        maximumUnits(box, 4);
    }
}
